package avicit.bdp.dcs.task.service;

import avicit.bdp.common.utils.enums.JobType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2020-11-22 11:43
 * @类说明：
 * @修改记录：
 */
@Component
public class TaskFactory {

    private static Map<Integer, TaskInterface> taskMap;

    private final StructureTaskService structureTaskService;
    private final StreamTaskService streamTaskService;
    private final ApiTaskService apiTaskService;
    private final FileTaskService fileTaskService;

    @Autowired
    public TaskFactory(StructureTaskService structureTaskService,
                       StreamTaskService streamTaskService,
                       ApiTaskService apiTaskService,
                       FileTaskService fileTaskService) {
        this.structureTaskService = structureTaskService;
        this.streamTaskService = streamTaskService;
        this.apiTaskService = apiTaskService;
        this.fileTaskService = fileTaskService;
    }


    public void init() {
        List<TaskInterface> list = new ArrayList<>();
        list.add(structureTaskService);
//        list.add(semiStructureTaskService);
        list.add(streamTaskService);
        list.add(apiTaskService);
        list.add(fileTaskService);
        taskMap = list.stream().collect(Collectors.toMap(TaskInterface::getType, taskInterface -> taskInterface));
    }

    public TaskInterface getTaskInterface(Integer jobType) {
        if (taskMap == null) {
            init();
        }
        if (jobType <= JobType.SYNC.getCode()) {
            return taskMap.get(JobType.TRANSFER.getCode());
        }
        return taskMap.get(jobType);
    }

}
